package com.chisheng.caa.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import java.util.List;

/**
 * Swagger 配置类
 */
@Configuration
@EnableSwagger2
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerConfig {

    @Resource
    private SwaggerProperties swaggerProperties;

    /**
     * 自动情况下启用 Swagger 的两种模式
     */
    public final static Profiles DEV_PROFILES = Profiles.of("dev", "test");

    @Bean
    public Docket defaultDocket(Environment environment) {
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        docket.groupName("0.0.1-DEV");
        docket.apiInfo(this.getApiInfo());

        if (swaggerProperties.isEnabled()) {
            docket.enable(true);
        } else {
            docket.enable(environment.acceptsProfiles(DEV_PROFILES));
        }

        return docket.select()
                .apis(RequestHandlerSelectors.basePackage("com.chisheng.caa.controller"))
                .build();
    }

    /**
     * 可以通过 apiInfo() 属性配置文档信息
     *
     * @return ApiInfo
     */
    public ApiInfo getApiInfo() {
        Contact contact = new Contact("Chisheng", "https://www.gitee.com/yunlm", "gndlwccw@163.com");
        return new ApiInfo(
                "CAA",
                "Curriculum Arrangement Assistance (CAA) for Honghe University.",
                "0.0.1-DEV",
                "https://gitee.com/yunlm/caa",
                contact,
                "Apache 2.0",
                "https://www.apache.org/licenses/LICENSE-2.0.html",
                List.of());
    }

}
